<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;

class IndexController extends CommonController
{
    //分配
    public function index(){
        
        return view('admin.index');
    }
    
    //欢迎界面
    public function welcome(){
        
        return view('admin.welcome');
    }
    //修改密码
    public function change_mima(){
//        dd(session('user'));
        if($input = Input::all()){
            $rules = [
                'password'=>'required|between:6,20|confirmed'//
            ];

            $messages = [
                'password.required'=>"新密码不能为空",
                'password.between'=>"密码在6到20位之间",
                'password.confirmed'=>'新密码与确认密码不一致'
            ];

            $validator = Validator::make($input,$rules,$messages);
            if($validator->passes()){
                $uname = session('user')['username'];
                $user = User::where('username',$uname)->first();
                if(Crypt::decrypt($user->password)!=$input['old_password']){
                    return back()->with('msg',"原密码错误");
                }
                $user->password=Crypt::encrypt($input['password']);
                $user->update();
                session(['user'=>null]);
                return "<script>parent.location.href='".url('admin/login')."'</script>";

            }else{
                
                return back()->withErrors($validator);
            }
        }
        return view('admin.change_mima');
    }






    
}
